package com.ztesoft.huangfu.learning.springboot.springbootredis.controller;

import com.ztesoft.huangfu.learning.springboot.springbootredis.entity.User;
import com.ztesoft.huangfu.learning.springboot.springbootredis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/5/26
 * Time: 17:54
 * Version: 1.0
 * Description: redis简单操作 redis 请求结果缓存
 **/
@Slf4j
@RestController
@RequestMapping("/redisuser")
public class RedisController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisService redisService;

    //http://localhost:8088/redisuser/get?key=ww&value=22
    // 简单的redis存取 ↓↓↓↓
    @PostMapping("/set")
    public void setRedisStringValue(String key, String value) {
        redisService.setRedisStringValue(key, value);
    }

    //http://localhost:8088/redisuser/get?key=huang
    @GetMapping("/get")
    public String getRedisStringValue(String key) {
        return redisService.getRedisStringValue(key);
    }
    // 简单的redis存取 ↑↑↑↑

    // redis缓存功能  ↓↓↓↓

    @GetMapping("/selectRedisUserById")
    public User selectRedisUserById(String id) {
        return redisService.selectUserById(id);
    }


    @PostMapping("/addUser")
    public int addUser(User user) {
        return redisService.addUser(user);
    }

    @DeleteMapping("/delete")
    public int delete(String id) {
        return redisService.delete(id);
    }

    @PostMapping("/update")
    public int update(User user) {
        return redisService.update(user);
    }

    @GetMapping("/clearUser1All")
    public void clearUser1All() {
        redisService.clearUser1All();
    }
    // redis缓存功能  ↑↑↑↑
    @GetMapping("/excuteLua")
    public Object excuteLua(){
        List keys = new ArrayList();
        keys.add("key1");
        keys.add("key2");
        List args = new ArrayList();
        args.add("2");
        args.add("20");

        System.out.println(System.currentTimeMillis());
        args.add("1");
        //增加输入输出序列化，否则set  get报错
        return redisTemplate.execute(redisRequestRateLimiterScript(),new StringRedisSerializer(),new StringRedisSerializer(),keys,args.toArray());
    }

    @Bean
    @SuppressWarnings("unchecked")
    public RedisScript redisRequestRateLimiterScript() {
        DefaultRedisScript redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("request_rate_limiter.lua")));
        redisScript.setResultType(List.class);
        return redisScript;
    }


    @Bean
    @SuppressWarnings("unchecked")
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

}
